home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / basic.arc / A3.TXT < prev    next >
Text File  |  1986-04-26  |  7KB  |  157 lines

  1. don't really understand about variables or the INPUT statement now,
  2. review the first two lessons before you continue with lesson three.
  3.  
  4.     If you are using the BASIC Prof,        |The PC-Prof.~
  5. let me know who you are!  Send your name        |P.O. Box 26~
  6. and address to:                        |Salina, Kansas~
  7.                             |67402-0026~
  8.  
  9.     If you like the Prof, include a contribution ($30 - $50 suggested) to
  10. help support development of additional volumes.  Please copy and share the
  11. Prof. with other IBM P.C. users.
  12. -----
  13. -----
  14. DATA and READ Statements
  15.  
  16.         In the last lesson you learned about the INPUT statement.  Another way
  17.   to tell the computer something is with |DATA~ and |READ~ statements.
  18.  
  19.         The DATA statement is |not~ executed.  Data is taken from it when the
  20. computer does a READ statement.  After the word DATA, you put numbers or groups
  21. of letters in quotes separating them with commas.  This creates a DATA line.
  22. The |first~ time the computer comes across a READ statement it will read the
  23. |first~ value in the DATA line; the |second~ time, the |second~ value, and so on.
  24.  
  25.         Try typing in and |RUN~ning |either~ of the following two examples.
  26.  
  27.         |10 DATA 7,11                   10 DATA "FRED","BARNEY"~
  28.         |20 READ X                      20 READ X$~
  29.         |30 PRINT X                     30 PRINT X$~
  30.         |40 READ X                      40 READ X$~
  31.         |50 PRINT X                     50 PRINT X$~
  32. -----
  33. DATA and READ Statements (continued)
  34.  
  35.         When the computer runs out of data, an |error~ stops the program and an
  36. |error message~ is displayed.  Try typing in the following program.
  37.  
  38.         |10 DATA 10,"Twenty"~
  39.         |20 READ X~
  40.         |30 PRINT 1,X~
  41.         |40 READ X$~
  42.         |50 PRINT 2,X$~
  43.         |60 READ X~
  44.         |70 PRINT 3,X~
  45.         
  46.         Because there is no third value to read, the computer runs out of data
  47. and stops, giving an |error~ in line number |60~.  |RUN~ it and see what happens.
  48. -----
  49. GOTO Statement
  50.  
  51.         The |GOTO~ statement is the simplest to understand, yet one of the most
  52. important statements in BASIC.  It tells the computer to go to a different line
  53. number instead of doing the next one in order.
  54.  
  55.         In the following sample program, the computer will |loop~ (re-do over
  56. and over) lines |20~, |30~ and |40~ until it runs out of data.
  57. -----
  58. IF THEN Statement
  59.  
  60.         The |IF THEN~ statement gives the computer a |choice~ of what to do.  |IF~ a
  61. condition is |true~ the computer does what follows the word |THEN~, |IF~ it is |not~
  62. |true~, the computer goes to the |next line~.  Usually a GOTO statement will
  63. follow the THEN.
  64.  
  65.         |Relational operators~ compare two values in an IF THEN statement. The
  66. most common relational operators follow.
  67.  
  68.         |=~  equal to           |<~ less than        |<=~ less than or equal to
  69.         |<>~ not equal to       |>~ greater than     |>=~ greater than or equal to
  70. -----
  71. IF THEN Statement (continued)
  72.  
  73.         By placing a unique number at the end of a DATA line, and following the
  74. READ statement with an IF THEN statement to test for that number, you can avoid
  75. the |Out of DATA~ error.  This program finds the average of the numbers in a
  76. DATA statement, and uses |99~ as an |end of data~ signal.
  77. -----
  78. IF THEN Statement (continued)
  79.  
  80.         Here are a couple sample programs.  One will count the numeric
  81. constants in a DATA statement.  The other will pick the numbers greater than 50
  82. out of a DATA statement, and print them.  In both cases if the number is |999~
  83. the computer will stop.  Try typing in and |RUN~ning them.
  84.  
  85.         |10 DATA 10,69,2,45,100,74,12,999       10 DATA 10,69,2,45,100,74,12,999~
  86.         |20 READ X                              20 READ X~
  87.         |30 IF X=999 THEN GOTO 60               20 IF X=999 THEN STOP~
  88.         |40 LET K=K+1                           40 IF X<=50 THEN GOTO 20~
  89.         |50 GOTO 20                             50 PRINT X~
  90.         |60 PRINT K                             60 GOTO 20~
  91. -----
  92. Logical Operators
  93.  
  94.         |Logical operators~ can make the IF THEN statement more versatile.  The
  95. logical operators in IBM BASIC are:
  96.  
  97.                          |AND~, |OR~, |NOT~, |XOR~, |IMP~, |EQV~
  98.         
  99.         The first two are pretty straight forward, and are the most widely
  100. used.  Consult your IBM BASIC manual for truth tables and explanations of the
  101. other operators.  The |AND~ and |OR~ operators are used when you want to test two
  102. conditions with an |IF THEN~ statement.  Here is an example of their use in a
  103. program.  Try typing it in and |RUN~ning it, then change the values in the DATA
  104. statement and |RUN~ it again.
  105.  
  106.                 |10 DATA 5,23,17,34,29,45,999~
  107.                 |20 READ X~
  108.                 |30 IF X=999 THEN GOTO 70~
  109.                 |40 IF X<20 OR X>30 THEN LET A=A+1~
  110.                 |50 IF X>=20 AND X<=30 THEN LET B=B+1~
  111.                 |60 GOTO 20~
  112.                 |70 PRINT A;"numbers either less than 20 or more than 30 and";~
  113.                 |B;"between 20 and 30."~
  114. -----
  115. ELSE Statement
  116.  
  117.         Another |option~ of the IF THEN statement is the |ELSE~ statement.
  118. Without the ELSE, the program would |drop~ through the IF THEN and go to the
  119. next line when the condition of the IF THEN is false.  If the word ELSE follows
  120. the IF THEN, it will be executed rather than dropping through to the next line.
  121.  
  122.         Here is the last program modified to show the ELSE statement.  Type it
  123. in and |RUN~ it.
  124.  
  125.                 |10 DATA 5,23,17,34,29,45,999~
  126.                 |20 READ X~
  127.                 |30 IF X=999 THEN GOTO 60~
  128.                 |40 IF X<20 OR X>30 THEN LET A=A+1 ELSE LET B=B+1~
  129.                 |50 GOTO 20~
  130.                 |60 PRINT A;"numbers either less than 20 or more than 30 and";~
  131.                 |B;"between 20 and 30."~
  132. -----
  133. Multiple Statement Lines
  134.  
  135.         Another useful feature of BASIC on the IBM is the use of |multiple~
  136. |statement~ lines.  You can put two or more statements on the same line number by
  137. separating them with a colon (|:~).  This is very helpful with IF THEN
  138. statements.
  139.  
  140.         Here is a sample program that illustrates the use of multiple statement
  141. lines.  Type it in and |RUN~ it, then change the values in the DATA statement and
  142. |RUN~ it again.
  143.  
  144.         |10 DATA 5,23,17,34,29,45,999~
  145.         |20 READ X~
  146.         |30 IF X=999 THEN PRINT A;"lower than 20,";B;"between 20 and 30, and";C;~
  147.         |"above 30.":END~
  148.         |40 IF X<20 THEN PRINT X;"is less than 20.":LET A=A+1:GOTO 20~
  149.         |50 IF X<30 THEN PRINT X;"is between 20 and 30.":LET B=B+1:GOTO 20~
  150.         |60 PRINT X;"is greater than 30.":LET C=C+1:GOTO 20~
  151. -----
  152. End of Lesson Three
  153.  
  154.         You have reached the end of lesson three.  This lesson contains some of
  155. the most important concepts of programming.  Loops and conditionals will most
  156. likely make up the major portion of any program you write.  Be sure you have a
  157. thorough understanding of this lesson before g